package com.data
{
	import mx.collections.ArrayCollection;
	import mx.events.CollectionEvent;
	import mx.events.CollectionEventKind;
	
	/**
	 * Klasse für Datenmodel einer Aktivität
	 */
	public class ActivityDAO implements IGraphElementDAO
	{
		/**
		* Ersteller der Aktivität
		*/
		private var _creator:String;
		/**
		* Liste der Personen mit vollen Zugriffsrechten
		*/
		private var _fullAccessList:ArrayCollection;
		/**
		* Liste der Personen mit eingeschränkten Zugriffsrechten
		*/
		private var _restrictedAccessList:ArrayCollection;
		
		private var _noAccessList:ArrayCollection;

		/**
		* Text der Aktivität
		*/
		private var _text:String = "Aktivität";
		/**
		* Liste mit allen parents
		*/
		private var _prev:Object;
		/**
		* Liste mit allen children
		*/
		private var _post:Object;
		
		/**
		* Eindeutiger Identifizierer
		*/
		private static var _uid:int = 0;
		
		/**
		* Zu versendende Nachricht bei Vollendung
		*/
		private var _message:String;
		
		private var _dataId:String;
		
		private var _decisionFunction:String;
		
		private var _deadline:String = "";
		
		private var _dataProvider:ArrayCollection;
		
		private var _accessRight:String;
		
		private var _finalized:Boolean;
		
		
		
		/**
		 * Konstruktor
		 */
		public function ActivityDAO()
		{
			_uid++;
			_dataId = "a"+_uid;
			_prev = {};
			_post = {};
			_fullAccessList = new ArrayCollection();
			_restrictedAccessList = new ArrayCollection();
			_noAccessList = new ArrayCollection();
			//_dataProvider = new ArrayCollection();
			
			
		}
		
		public function set creator(value:String):void
		{
			_creator = value;
		}
		public function get creator():String
		{
			return _creator;
		}
		
		public function set text(value:String):void
		{
			_text = value;
		}
		public function get text():String
		{
			return _text;
		}
		
		public function get dataId():String
		{
			return _dataId;
		}
		
		public function set dataId(value:String):void
		{
			_dataId = value;
		}
		
		public function set message(value:String):void
		{
			_message = value;
		}
		public function get message():String
		{
			return _message;
		}
		
		public function set deadline(value:String):void
		{
			_deadline = value;
		}
		public function get deadline():String
		{
			return _deadline;
		} 
		
		public function set accessRight(value:String):void
		{
			_accessRight = value;
		}
		public function get accessRight():String
		{
			return _accessRight;
		}
		
		public function set finalized(value:Boolean):void
		{
			_finalized = value;
		}
		public function get finalized():Boolean
		{
			return _finalized;
		}
		
		
		/**
		 * Hinzufügen eines parents
		 * @param obj Activity oder Decision Objekt
		 * 
		 */
		public function addPrev(uid:String):void
		{
			if(!_prev[uid])
				_prev[uid] = 1;
			else
				throw Error("Objekt kann nicht zur prev Liste hinzugefügt werden, " + 
						"da es schon in ihr vorhanden ist");
		}
		
		/**
		 * Entfernen eines parents
		 * @param obj Activity oder Decision Objekt
		 * 
		 */
		public function removePrev(uid:String):void
		{
			if(!_prev[uid])
				throw Error("Objekt kann nicht von prev Liste entfernt werden, " + 
						"da es nicht in ihr vorhanden ist");
			else
				_prev[uid] = null;
		}
		
		/**
		 * Hinzufügen eines child
		 * @param obj Activity oder Decision Objekt
		 * 
		 */
		public function addPost(uid:String):void
		{
			if(!_post[uid])
				_post[uid] = 1;
			else
				throw Error("Objekt kann nicht zur post Liste hinzugefügt werden, " + 
						"da es schon in ihr vorhanden ist");
		}
		
		public function get prevList():Object
		{
			return _prev;
		}
		
		public function get postList():Object
		{
			return _post;
		}
		
		/**
		 * Entfernen eines child
		 * @param obj Activity oder Decision Objekt
		 * 
		 */
		public function removePost(uid:String):void
		{
			if(!_prev[uid])
				throw Error("Objekt kann nicht von post Liste entfernt werden, " + 
						"da es nicht in ihr vorhanden ist");
			else
				_prev[uid] = null;
		}
		
		public function set fullAccessList(value:ArrayCollection):void
		{
			_fullAccessList = value;
		}
		public function get fullAccessList():ArrayCollection
		{
			return _fullAccessList;
		}
		
		public function set restrictedAccessList(value:ArrayCollection):void
		{
			_restrictedAccessList = value;
		}
		public function get restrictedAccessList():ArrayCollection
		{
			return _restrictedAccessList;
		}
		
		public function set noAccessList(value:ArrayCollection):void
		{
			_noAccessList = value;
		}
		public function get noAccessList():ArrayCollection
		{
			return _noAccessList;
		}
		
		public function set decisionFunction(value:String):void
		{
			_decisionFunction = value;
		}
		public function get decisionFunction():String
		{
			return _decisionFunction;
		}
		
		
		private function accessListIndex(pName:String, pList:Array):int
		{
			var result:int = -1;
			
			for(var i:int = 0; i < pList.length; i++)
			{
				if(pList[i] == pName)
				{
					result = i;
					break;
				}
			}
			
			return result;
		}
		
		public function set dataProvider(value:ArrayCollection):void
		{
			_dataProvider = value;
			_dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangeHandler);
		}
		public function get dataProvider():ArrayCollection
		{
			return _dataProvider;
		}
		
		private function collectionChangeHandler(evt:CollectionEvent):void
		{
			var evtKind:String = evt.kind;
			switch(evtKind)
			{
				case CollectionEventKind.ADD:
					addItems(evt.items);
					break;
				case CollectionEventKind.REMOVE:
					removeItems(evt.items);
					break;
			}
		}
		private function addItems(pItems:Array):void
		{
			for(var i:int = 0; i < pItems.length; i++)
			{
				if(!_noAccessList.contains(pItems[i]))
				{	
					_noAccessList.addItem(pItems[i]);
				}
			}
		}
		private function removeItems(pItems:Array):void
		{
			for(var i:int = 0; i < pItems.length; i++)
			{
				if(_noAccessList.contains(pItems[i]))
				{		
					var index:int = _noAccessList.getItemIndex(pItems[i]);
					_noAccessList.removeItemAt(index);
				}
			}
		}
	}
}